import 'package:flutter/material.dart';
import 'package:untitled/utils/SnackBarHepler.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      scaffoldMessengerKey: ScaffoldMessengerGlobalKey,
      title: 'Flutter Demo',
      theme: ThemeData(
        colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
      ),
      home: const MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key, required this.title});

  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage>
    with SingleTickerProviderStateMixin {
  @override
  Widget build(BuildContext context) {
    //脚手架
    return Scaffold(
      appBar: AppBar(
        title: Text('单选框'),
        backgroundColor: Colors.blue,
        foregroundColor: Colors.white,
      ),

      body: SizedBox.expand(
        child: Column(
          children: [
            Divider(),

            buildRadioGroup(),

            Divider(),
            buildColumn(),
            Divider(),
            buildColumn2(),
          ],
        ),
      ),
    );
  }

  int _selectNew = 0;

  RadioGroup<int> buildRadioGroup() {
    return RadioGroup(
      onChanged: (int? index) {
        setState(() {
          _selectNew = index ?? 0;
        });
      },
      groupValue: _selectNew,
      child: Column(
        children: [
          Row(children: [Radio(value: 0), Text('数学')]),
          Row(children: [Radio(value: 1), Text('语文')]),
          Row(
            children: [
              Radio(value: 2, activeColor: Colors.red),
              Text('英文'),
            ],
          ),
        ],
      ),
    );
  }

  Column buildColumn2() {
    return Column(
      mainAxisSize: MainAxisSize.min,
      children: [
        RadioListTile<int>(
          title: Text('男'),
          // 主标题
          value: 0,
          secondary: Icon(Icons.man),
          groupValue: _selectGender,
          onChanged: (int? value) {
            setState(() {
              _selectGender = value ?? 0;
            });
          },
        ),
        RadioListTile<int>(
          title: Text('女'),
          // 主标题
          value: 1,
          activeColor: Colors.red,
          tileColor: Colors.grey[100],
          radioBackgroundColor: WidgetStateProperty.resolveWith((states) {
            // 1. hovered - 鼠标悬停状态
            if (states.contains(WidgetState.hovered)) {
              return Colors.blue.shade50.withOpacity(0.8);
            }

            // 2. focused - 键盘导航获得焦点状态
            if (states.contains(WidgetState.focused)) {
              return Colors.green.shade100;
            }

            // 3. pressed - 按下状态
            if (states.contains(WidgetState.pressed)) {
              return Colors.red.shade100;
            }

            // 4. dragged - 拖动状态（在Radio中不常见）
            if (states.contains(WidgetState.dragged)) {
              return Colors.purple.shade100;
            }

            // 5. selected - 选中状态
            if (states.contains(WidgetState.selected)) {
              return Colors.orange.shade100;
            }

            // 6. scrolledUnder - 滚动到下方状态
            if (states.contains(WidgetState.scrolledUnder)) {
              return Colors.brown.shade100;
            }

            // 7. disabled - 禁用状态
            if (states.contains(WidgetState.disabled)) {
              return Colors.grey.shade300;
            }

            // 8. error - 错误状态
            if (states.contains(WidgetState.error)) {
              return Colors.red.shade200;
            }

            return Colors.transparent; // 默认状态
          }),
          fillColor: WidgetStateProperty.all(Colors.red),
          subtitle: Text('测试 你好啊'),
          secondary: Icon(Icons.woman),
          groupValue: _selectGender,
          onChanged: (int? value) {
            setState(() {
              _selectGender = value ?? 1;
            });
          },
        ),
      ],
    );
  }

  //0 男  1 女
  int _selectGender = 0;

  Column buildColumn() {
    return Column(
      mainAxisSize: MainAxisSize.min,
      children: [
        //
        Row(
          children: [
            Radio<int>(
              value: 0,
              groupValue: _selectGender,
              onChanged: (int? value) {
                setState(() {
                  _selectGender = value ?? 0;
                });
              },
            ),
            GestureDetector(
              child: Text('男'),
              onTap: () {
                setState(() {
                  _selectGender = 0;
                });
              },
            ),
          ],
        ),
        //
        Row(
          children: [
            Radio(
              value: 1,
              groupValue: _selectGender,
              onChanged: (int? value) {
                setState(() {
                  _selectGender = value ?? 1;
                });
              },
            ),
            GestureDetector(
              child: Text('女'),
              onTap: () {
                setState(() {
                  _selectGender = 1;
                });
              },
            ),
          ],
        ),
      ],
    );
  }
}
